﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using System.Web;
using FacturIT.BLL.Model.Helper;
using FacturIT.BLL.Model.PaymentType;
using FacturIT.Service;

namespace FacturIT.Service.Repositories.PaymentType
{
    public class PaymentTypesRepository : IRepository<FacturIT.BLL.Model.PaymentType.PaymentType, int>
    {
        public PaymentTypesRepository()
        {
            DB = new PaymentTypeManagementDataContext(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["DatabaseToUse"]].ConnectionString);
        }

        protected Table<FacturIT.BLL.Model.PaymentType.PaymentType> DT
        {
            get { return ((PaymentTypeManagementDataContext)base.DB).PaymentTypes; }
        }

        public override FacturIT.BLL.Model.PaymentType.PaymentType Read(int id)
        {
            FacturIT.BLL.Model.PaymentType.PaymentType paymentType = ReadByID(id);
            return paymentType == null ? null : paymentType;
        }
        public FacturIT.BLL.Model.PaymentType.PaymentType Read(string name)
        {
            return (from paymentType in DT
                    where paymentType.Name.ToLower().Equals(name.ToLower())
                    select paymentType).FirstOrDefault();
        }

        public override IEnumerable<FacturIT.BLL.Model.PaymentType.PaymentType> ReadAll()
        {
            return from paymentType in DT
                   orderby paymentType.Name
                   select paymentType;
        }

        public override IEnumerable<FacturIT.BLL.Model.PaymentType.PaymentType> ReadAll(params int[] ids)
        {
            return from paymentType in DT
                   where ids.Contains(paymentType.PaymentTypeID)
                   select paymentType;
        }
        internal IEnumerable<FacturIT.BLL.Model.PaymentType.PaymentType> ReadAll(List<int> assetIDs)
        {
            return from paymentType in DT
                   where assetIDs.Contains(paymentType.PaymentTypeID)
                   select paymentType;
        }
        

        public override FacturIT.BLL.Model.PaymentType.PaymentType Add(FacturIT.BLL.Model.PaymentType.PaymentType t)
        {
            DT.InsertOnSubmit(t);
            SaveChanges();
            return ReadByID(t.PaymentTypeID);
        }

        public override bool Delete(FacturIT.BLL.Model.PaymentType.PaymentType t)
        {
            DT.DeleteOnSubmit(t);
            SaveChanges();
            return !Exists(t.PaymentTypeID);
        }

        public override FacturIT.BLL.Model.PaymentType.PaymentType Update(FacturIT.BLL.Model.PaymentType.PaymentType t)
        {
            UpdateObject(ReadByID(t.PaymentTypeID), t, false);
            SaveChanges();
            return ReadByID(t.PaymentTypeID);
        }

        public override FacturIT.BLL.Model.PaymentType.PaymentType Duplicate(FacturIT.BLL.Model.PaymentType.PaymentType t)
        {
            return Add(Create(t.Name));
        }

        protected override FacturIT.BLL.Model.PaymentType.PaymentType ReadByID(int id)
        {
            return DT.SingleOrDefault(x => x.PaymentTypeID == id);
        }

        protected override void UpdateObject(FacturIT.BLL.Model.PaymentType.PaymentType left, FacturIT.BLL.Model.PaymentType.PaymentType right, bool replaceID)
        {
            if (replaceID) left.PaymentTypeID = right.PaymentTypeID;
            left.Name = right.Name;
        }

        protected override IEnumerable<FacturIT.BLL.Model.PaymentType.PaymentType> ApplyOrdering(IEnumerable<FacturIT.BLL.Model.PaymentType.PaymentType> query, IDictionary<Func<FacturIT.BLL.Model.PaymentType.PaymentType, object>, Enum.Ordering> columns)
        {
            return OrderHelper.ApplyOrdering<FacturIT.BLL.Model.PaymentType.PaymentType, object>(query, columns, paymentType => paymentType.PaymentTypeID);
        }

        protected FacturIT.BLL.Model.PaymentType.PaymentType Create(string name)
        {
            return new FacturIT.BLL.Model.PaymentType.PaymentType()
            {
                Name = name,
            };
        }

        internal BLL.Model.PaymentType.PaymentType ReadPaymentType(int paymentTypeID)
        {
            return (from paymenttype in DT
                    where paymenttype.PaymentTypeID == paymentTypeID
                    select paymenttype).FirstOrDefault();
        }
    }
}